package com.ruoyi.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.system.domain.MemberPackageConfig;
import com.ruoyi.system.domain.MemberPackageItem;
import com.ruoyi.system.domain.Order;
import com.ruoyi.system.domain.vo.OrderSearchVo;
import com.ruoyi.system.domain.vo.OrderVo;
import com.ruoyi.system.dto.OrderDTO;
import com.ruoyi.system.mapper.MemberPackageConfigMapper;
import com.ruoyi.system.mapper.MemberPackageItemMapper;
import com.ruoyi.system.mapper.OrderMapper;
import com.ruoyi.system.service.IOrderService;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements IOrderService {
    @Autowired
    private MemberPackageItemMapper itemMapper;
    @Autowired
    private MemberPackageConfigMapper configMapper;
    @Override
    public List<OrderDTO> getList(OrderSearchVo searchVo) {
        LambdaQueryWrapper<Order> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(searchVo.getUserId()), Order::getUserId, searchVo.getUserId());
        wrapper.eq(searchVo.getPayStatus() != null, Order::getPayStatus, searchVo.getPayStatus());
        wrapper.and(StringUtils.isNotBlank(searchVo.getUserInfo()),w ->
            w.like(Order::getUserPhone, searchVo.getUserInfo())
                .or().like(Order::getUserName, searchVo.getUserInfo()));
        wrapper.eq(searchVo.getPurchaseType()!=null, Order::getPurchaseType, searchVo.getPurchaseType());
        wrapper.orderByDesc(Order::getCreateTime);
        List<Order> orders = baseMapper.selectList(wrapper);
        List<OrderDTO> dtos = new ArrayList<>();
        for (Order order : orders) {
            OrderDTO orderDTO = new OrderDTO();
            BeanUtils.copyProperties(order,orderDTO);
            if (order.getProductId() != null){
                MemberPackageItem memberPackageItem = itemMapper.selectOne(new LambdaQueryWrapper<MemberPackageItem>().eq(MemberPackageItem::getItemId,order.getProductId()));
                if (memberPackageItem != null){
                    orderDTO.setItem(memberPackageItem);
                    MemberPackageConfig memberPackageConfig = configMapper.selectById(memberPackageItem.getConfigId());
                    if (memberPackageConfig != null){
                        orderDTO.setPackageName(memberPackageConfig.getPackageName());
                    }
                }
            }
            dtos.add(orderDTO);
        }
        return dtos;
    }

    @Override
    public Boolean addOrUpdate(OrderVo orderVo) {
        Order order = new Order();
        BeanUtils.copyProperties(orderVo, order);
        return baseMapper.insert(order)>0;
    }

    @Override
    public long getListCount(OrderSearchVo searchVo) {
        LambdaQueryWrapper<Order> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(searchVo.getUserId()), Order::getUserId, searchVo.getUserId());
        wrapper.eq(searchVo.getPayStatus() != null, Order::getPayStatus, searchVo.getPayStatus());
        wrapper.and(StringUtils.isNotBlank(searchVo.getUserInfo()),w ->
            w.like(Order::getUserPhone, searchVo.getUserInfo())
                .or().like(Order::getUserName, searchVo.getUserInfo()));
        return baseMapper.selectCount(wrapper);
    }
}
